package com.yc.tc.utils;

public class PageUtil {
	private int pageNo = 1; // 当前是第几页
	private int pageSize = 5; // 每页显示的条数
	private int totalPage = 1; // 总页数
	private int totalSize = 0; // 总记录数

	@Override
	public String toString() {
		return "PageUtil [pageNo=" + pageNo + ", pageSize=" + pageSize
				+ ", totalPage=" + totalPage + ", totalSize=" + totalSize + "]";
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		if (pageNo < 1) { // 如果已经是第一页了，则不再往下
			pageNo = 1;
		} else if (pageNo > totalPage) { // 如果已经是最后一页了，则不再往上
			pageNo = totalPage;
		} else {
			this.pageNo = pageNo;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if (pageSize <= 0) { // 如果给定的每页显示的数据不合法，则默认每页显示5条
			this.pageSize = 5;
		} else {
			this.pageSize = pageSize;
		}
	}

	public int getTotalSize() {
		return totalSize;
	}

	public void setTotalSize(int totalSize) {
		this.totalSize = totalSize;
	}

	public int getTotalPage() { // 总页数
		totalPage = this.totalSize % this.pageSize == 0 ? this.totalSize
				/ this.pageSize : this.totalSize / this.pageSize + 1;
		return totalPage;
	}

	public PageUtil() {
		super();
	}

	public PageUtil(int pageNo, int pageSize, int totalPage, int totalSize) {
		super();
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.totalPage = totalPage;
		this.totalSize = totalSize;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + pageNo;
		result = prime * result + pageSize;
		result = prime * result + totalPage;
		result = prime * result + totalSize;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PageUtil other = (PageUtil) obj;
		if (pageNo != other.pageNo)
			return false;
		if (pageSize != other.pageSize)
			return false;
		if (totalPage != other.totalPage)
			return false;
		if (totalSize != other.totalSize)
			return false;
		return true;
	}

}
